package design2.chain_of_responsibility.example;

import design2.chain_of_responsibility.example.middleware.Middleware;
import design2.chain_of_responsibility.example.middleware.RoleCheckMiddleware;
import design2.chain_of_responsibility.example.middleware.ThrottlingMiddleware;
import design2.chain_of_responsibility.example.middleware.UserExistsMiddleware;
import design2.chain_of_responsibility.example.server.LoginServer;
import design2.chain_of_responsibility.example.server.UserInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 演示课。这里一切都在一起。
 */
public class Demo {


	private static BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

	private static LoginServer loginServer;

	private static UserInfo userInfo;

	private static void init(){
		//初始化一些用户
		userInfo =new UserInfo();
		userInfo.register("qing@126.com","123456");
		userInfo.register("hello@126.com","123456");

		//所有支票都是链接的。客户机可以使用相同的方法构建各种链
		Middleware middleware=new ThrottlingMiddleware(2);
		middleware.linkWith(new UserExistsMiddleware(userInfo))
				.linkWith(new RoleCheckMiddleware());

		//服务器从客户端代码获取一个链。
		loginServer=new LoginServer(middleware);
	}

	public static void main(String[] args) throws IOException {
		init();
		boolean success;
		do{
			System.out.println("请输入邮箱:");
			String email= reader.readLine();
			System.out.println("请输入密码：");
			String password=reader.readLine();
			success= loginServer.login(email,password);
		}while (!success);
	}
}
